package com.example.myapplication.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.myapplication.R;
import com.example.myapplication.tools.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyGridViewTest extends Activity {

    GridView gridView;
    Context context;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gridtest);
        context = this;
        gridView = findViewById(R.id.gv_test);
        gridView.setNumColumns(5);
        RadioGroup rg = findViewById(R.id.rg_test);
        rg.setOnCheckedChangeListener((view,childId)->{
            RadioButton rb = findViewById(childId);
            ToastUtils.showMessages(rb.getText().toString(),context);
        });
        initGridViewData();
    }


    MyAdapter adapter;
    void initGridViewData(){
        List<Map<String,String>> list = new ArrayList<>();
        for(int i = 0 ; i < 6 ; i ++){
            Map<String,String> map = new HashMap();
            map.put("name" ,"测试" + i);
            map.put("pic" ,"测试" + i);
            list.add(map);
        }
        adapter = new MyAdapter(this,list);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
                Map<String,String> map = (Map<String, String>) parent.getItemAtPosition(i);
                ToastUtils.showMessages(map.get("name").toString(),context);
            }
        });
        gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int ii, long l) {

                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("提示");
                builder.setMessage("是否要删除此条数据");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        adapter.removeView(ii);
                    }
                });
                builder.setNegativeButton("关闭",null);
                AlertDialog ad = builder.create();
                ad.setCanceledOnTouchOutside(false);
                ad.show();
                return true;
            }
        });
    }

    class MyAdapter extends BaseAdapter {

        List<Map<String,String>> list;

        MyAdapter(Context context, List<Map<String,String>> _list){
            list = _list;
        }

        public void removeView(int position){
            list.remove(position);
            this.notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if(view == null){
                view = getLayoutInflater().inflate(R.layout.item_iv_tv_grid,null);
            }
            TextView tv = view.findViewById(R.id.item_tv);
            ImageView iv = view.findViewById(R.id.item_iv);
            Map<String,String> map = list.get(i);
            tv.setText(map.get("name"));
            iv.setImageResource(R.drawable.pic_item_task);
            return view;
        }
    }
}
